(function () {
    'use strict';
    var global$7 = tinymce.util.Tools.resolve('tinymce.PluginManager');
    var unlinkSelection = function (editor) {
        var dom = editor.dom, selection = editor.selection;
        var bookmark = selection.getBookmark();
        var rng = selection.getRng().cloneRange();
        var startAnchorElm = dom.getParent(rng.startContainer, 'a[href]', editor.getBody());
        var endAnchorElm = dom.getParent(rng.endContainer, 'a[href]', editor.getBody());
        if (startAnchorElm) {
            rng.setStartBefore(startAnchorElm);
        }
        if (endAnchorElm) {
            rng.setEndAfter(endAnchorElm);
        }
        selection.setRng(rng);

        if (editor.selection.isCollapsed()) {
            var elm = editor.dom.getParent(editor.selection.getStart(), 'a');
            if (elm) {
                editor.dom.remove(elm, true);
            }
            return;
        }
        editor.formatter.remove('link');

        selection.moveToBookmark(bookmark);
    };
    var getHref = function (elm) {
        var href = elm.getAttribute('data-mce-href');
        return href ? href : elm.getAttribute('href');
    };
    var gotoLink = function (editor, a) {
        if (a) {
            var href = getHref(a);
            if (/^#/.test(href)) {
                var targetEl = editor.$(href);
                if (targetEl.length) {
                    editor.selection.scrollIntoView(targetEl[0], true);
                }
            } else {
                open(a.href);
            }
        }
    };
    var getLink = function (editor, elm) {
        return editor.dom.getParent(elm, 'a[href]');
    };
    var getSelectedLink = function (editor) {
        return getLink(editor, editor.selection.getStart());
    };
    var gotoSelectedLink = function (editor) {
        return function () {
            gotoLink(editor, getSelectedLink(editor));
        };
    };
    var unlinkDomMutation = function (editor) {
        editor.undoManager.transact(function () {
            var node = editor.selection.getNode();
                unlinkSelection(editor);
                editor.focus();
        });
    };
    var setupMenuItems = function (editor) {
        editor.ui.registry.addMenuItem('makeEditable', {
            text: 'makeEditable',
            icon: 'new-tab',
            onAction: function (){
                var tdObject = editor.dom.getParent(editor.selection.getStart(), "td");
                editor.execCallback('makeEditableCallBack', editor,tdObject);
                editor.undoManager.add();
            }
        });
        editor.ui.registry.addMenuItem('removeMakeEditable', {
            icon: 'unlink',
            text: 'removeMakeEditable',
            onAction: function () {
                var tdObject = editor.dom.getParent(editor.selection.getStart(), "td");
                editor.execCallback('removeMakeEditableCallBack', editor,tdObject);
            },
        });
    };
    var setupContextMenu = function (editor) {
        editor.ui.registry.addContextMenu('makeEditable', {
            update: function (element) {
                var tdObject = editor.dom.getParent(editor.selection.getStart(), "td");
                if(!tdObject) return "";
                if(tdObject.getAttribute('sourcetype') != 1) return ""
                if(tdObject.getAttribute('isWrite') == 1) return "removeMakeEditable";
                return "makeEditable";
            }
        });
    };
    function Plugin () {
        global$7.add('makeEditable', function (editor) {
            setupMenuItems(editor);
            setupContextMenu(editor);
        });
    }

    Plugin();
}());